<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
cfc('core')->checklogin();
$return['role']=$_SESSION['role'];
$return['member']=array();
if($_RQ['op']=='list'){
	$member=pdo_get('jy_taxi_driver',array('mid'=>$_SESSION['uid']));
	if(!$member){
		$member=pdo_get('jy_taxi_member',array('mid'=>$_SESSION['uid']));
		if(!$member){
		$member['user']='no';
		}else{
		$member['user']='member';
		}
	}else{
		$member['user']='driver';
	}
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	if($member['user']=='member'){
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']);
	}else{
		$params=array('uniacid'=>$_SESSION['uniacid'],'driver_id'=>$member['id']);
	}
	//$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);	
    $fields=array('id','ordersn','name','mobile','driver_id','mid','createtime','status','price','price1','start_seat','dest_seat','cuid');
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_taxi_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	
	if($return['list']){
		foreach($return['list'] as &$v){
			if($v['cuid'] && $v['cuid']!=$_SESSION['uid']){
				$v['cel']=1;
			}
			$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		}
	}
	$return['type']=array('0'=>'已下单','1'=>'已接单','2'=>'已申请取消','3'=>'已取消','4'=>'已退款','5'=>'已完成','6'=>'等待司机接单中');
	$return['user']=$member['user'];
	$info=model('core/member')->basic($_SESSION['uid']);
	$member['nickname']=$info['nickname'];
	$member['avatar']=$info['avatar'];
	$member['credit2']=$info['credit2'];
	$return['member']=$member;
	exi($return);
}	
if($_RQ['op']=='del'){
	
	$id=intval($_RQ['id']);
	if($id){
		$order=pdo_get('jy_taxi_order',array('id'=>$id));
		
		pdo_delete('jy_taxi_order',array('id'=>$id));	
	}
	
	exi('删除成功！');

}
if($_RQ['op']=='cel'){

	$id=intval($_RQ['id']);
	
	if($id){
		$order=pdo_get('jy_taxi_order',array('id'=>$id));
		if($order['status']==1){
			pdo_update('jy_taxi_order',array('status'=>2,'cuid'=>$_SESSION['uid']),array('id'=>$id));	
		}else{
			pdo_update('jy_taxi_order',array('status'=>3,'cuid'=>$_SESSION['uid']),array('id'=>$id));	
		}
	}
	
	exi('已申请取消,请联系对方确认');

}
if($_RQ['op']=='dcel'){

	$id=intval($_RQ['id']);
	
	if($id){
		$order=pdo_get('jy_taxi_order',array('id'=>$id));
		
		pdo_update('jy_taxi_order',array('status'=>3),array('id'=>$id));
		$driver_id=pdo_getcolumn('jy_taxi_order',array('id'=>$id),'driver_id');
		pdo_update('jy_taxi_driver',array('busy'=>1),array('id'=>$driver_id));
	}
	
	exi('已确定取消');

}
if($_RQ['op']=='done'){

	$id=intval($_RQ['id']);
	
	if($id){
		$order=pdo_get('jy_taxi_order',array('id'=>$id));
		if($order['status']!=1 ){
			exi('未确认订单无法完成','error');
		}
		$set=$this->plugin_setting('setting');
		$bili=$set['credit']?$set['credit']/100:0.2;
		$credit=round($order['price']*$bili,2);
		pdo_update('core_members',array('credit2 -='=>$credit),array('id'=>$order['driver_id']));
		pdo_update('jy_taxi_order',array('status'=>5),array('id'=>$id));
		$driver_id=pdo_getcolumn('jy_taxi_order',array('id'=>$id),'driver_id');
		pdo_update('jy_taxi_driver',array('busy'=>1),array('id'=>$driver_id));
		
	}
	exi('已完成');

}
if($_RQ['op']=='detail'){

	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单','error','list');
	}
	
	$return['item']=pdo_get('jy_taxi_order',array('id'=>$id));
	$return['item']['createtime']=date('Y-m-d H:i:s',$return['item']['createtime']);
	
	
	exi($return);
}
?>